﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace WindowsFormsApplication3
{
    public partial class QuotationForm : Form
    {
        private bool nonNumberEntered = false;
          private String connString = Program.ConnStr;
        public QuotationForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// load combobox with data from database
        /// 
        /// </summary>

        public void comboboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobcode from  jobcodemastertable",oleDbConnection1);
            OleDbDataReader reader = oleDbCommand1.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("jobpk", typeof(int));
            dt.Columns.Add("jobcode", typeof(string));
            dt.Load(reader);
            cmbjobcode.ValueMember = "jobpk";
            cmbjobcode.DisplayMember = "jobcode";
            cmbjobcode.DataSource = dt.DefaultView;
           
            oleDbConnection1.Close();

       }

        private void QuotationForm_Load(object sender, EventArgs e)
        {
         
            comboboxload();
            newquotationoget();
            
        }


        public Boolean validationcontrol()
        {
            Boolean sucess = false;
            if (cmbjobcode.Text.Trim() == null || cmbjobcode.Text.Trim() == "")
            {
                lblstatus.Text = "Enter Jobcode";
                cmbjobcode.Focus();
                cmbjobcode.Text = "";
            }
            else if (txtquotationno.Text.Trim() == null || txtquotationno.Text.Trim() == "")
            {
                lblstatus.Text = "Enter An Quotation Number";
                txtquotationno.Focus();
               
                txtquotationno.Text = "";
            }

            else if (txtsilabordercharges.Text.Trim() == null || txtsilabordercharges.Text.Trim() == "")
            {
                lblstatus.Text = "Enter Silla BorderCharges";
                txtsilabordercharges.Focus();
               
                txtsilabordercharges.Text = "";
            }
            else if (txtbattacharges.Text.Trim() == null || txtbattacharges.Text.Trim() == "")
            {
                lblstatus.Text = "Enter Batta Charges";
                txtbattacharges.Focus();
               
                txtbattacharges.Text = "";
            }

            else if (txtdriverpayment.Text.Trim() == null || txtdriverpayment.Text.Trim() == "")
            {
                lblstatus.Text = "Enter Driver Payment";
                txtdriverpayment.Focus();
                
                txtdriverpayment.Text = "";
            }

            else if (txtrent.Text.Trim() == null || txtrent.Text.Trim() == "")
            {
                lblstatus.Text = "Confirm rent ";
                txtrent.Focus();
              
                txtrent.Text = "";
            }

            else if (txtextra.Text.Trim() == null || txtextra.Text.Trim() == "")
            {
                lblstatus.Text = "Enter Extra charges";
                txtextra.Focus();
               
                txtextra.Text = "";
            }

            else if (txttotal.Text.Trim() == null || txttotal.Text.Trim() == "")
            {
                lblstatus.Text = "Total Not Calculated : Failure";
                txttotal.Focus();
                
                txttotal.Text = "";
            }
            else if (txtdiscount.Text.Trim() == null || txtdiscount.Text.Trim() == "")
            {
                lblstatus.Text = "Enter Discount ";
                txtdiscount.Focus();
               
                txtdiscount.Text = "";
            }
            else if (txtamount.Text.Trim() == null || txtamount.Text.Trim() == "")
            {

             
                lblstatus.Text = "Failed Calculating Discount";
                txtamount.Focus();

                txtamount.Text = "";
            }
            

            else
            {

                sucess = true;
            }
            return sucess;
        }






        /// <summary>
        /// get new quotationno autogenerated
        /// </summary>


        public void newquotationoget()
        {

            Int32 S = 0;
            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();


            String query = "SELECT TOP 1 quotationpk FROM quotationmastertable ORDER BY quotationpk DESC";
          
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);

           
            DataSet ds = new DataSet();
           

            dAdapter.Fill(ds, "tblquotationdataview");
            if (ds.Tables[0].Rows.Count == 0)
            {
                S = 0;
            }
            else
            {
                S = int.Parse(ds.Tables[0].Rows[0][0].ToString());
            }
           
           
            S++;

           

            txtquotationno.Text = "QUOT" + S.ToString();
            txtquotationno.Enabled = false;

            oleDbConnection1.Close();
        }



        public void insidekeypress(KeyPressEventArgs e)
        {

            // Check for the flag being set in the KeyDown event.
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
                lblstatus.Text = "Enter A valid Number";

            }
            else
            {
                if (e.KeyChar == '\t')
                {
                    lblstatus.Text = "";
                   
                }

            }
                
        }



       public void insidekeydown(KeyEventArgs e)
        {

            // Initialize the flag to false.
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace.
                    if (e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                    }
                }


            }
        }






        private void txtsilabordercharges_KeyPress(object sender, KeyPressEventArgs e)
        {
            insidekeypress(e);
            txtsilabordercharges.Focus();

          
        }

        private void txtsilabordercharges_KeyDown(object sender, KeyEventArgs e)
        {
            insidekeydown(e);
            txtsilabordercharges.Focus();

          
        }

        private void txtbattacharges_KeyPress(object sender, KeyPressEventArgs e)
        {
            insidekeypress(e);
            txtbattacharges.Focus();

            
        }

        private void txtdriverpayment_KeyPress(object sender, KeyPressEventArgs e)
        {
            insidekeypress(e);
            txtdriverpayment.Focus();

            
        }

        private void txtrent_KeyPress(object sender, KeyPressEventArgs e)
        {
            insidekeypress(e);
            txtrent.Focus();

      

        }

        private void txtextra_KeyPress(object sender, KeyPressEventArgs e)
        {
            insidekeypress(e);
            txtextra.Focus();

           
        }

        private void txttotal_KeyPress(object sender, KeyPressEventArgs e)
        {
            insidekeypress(e);
            txttotal.Focus();

          
        }

        private void txtdiscount_KeyPress(object sender, KeyPressEventArgs e)
        {
            insidekeypress(e);
            txtdiscount.Focus();

           
        }

        private void txtamount_KeyPress(object sender, KeyPressEventArgs e)
        {
            insidekeypress(e);
            txtamount.Focus();

            
        }

        private void txtbattacharges_KeyDown(object sender, KeyEventArgs e)
        {
            insidekeydown(e);
            txtbattacharges.Focus();

            
        }

        private void txtdriverpayment_KeyDown(object sender, KeyEventArgs e)
        {
            insidekeydown(e);
            txtdriverpayment.Focus();

           
        }

        private void txtrent_KeyDown(object sender, KeyEventArgs e)
        {
            insidekeydown(e);
            txtrent.Focus();

            
        }

        private void txtextra_KeyDown(object sender, KeyEventArgs e)
        {
            insidekeydown(e);
            txtextra.Focus();

            
            
        }

        private void txttotal_KeyDown(object sender, KeyEventArgs e)
        {
            insidekeydown(e);
            txttotal.Focus();

            
        }

        private void txtdiscount_KeyDown(object sender, KeyEventArgs e)
        {
            insidekeydown(e);
            txtdiscount.Focus();

           

        }

        private void txtamount_KeyDown(object sender, KeyEventArgs e)
        {
            insidekeydown(e);
            txtamount.Focus();

            
        }

        /// <summary>
        /// add the values in the textbox
        /// 
        /// </summary>
        public void addaction()
        {
            int total = int.Parse(txtsilabordercharges.Text) + int.Parse(txtbattacharges.Text) + int.Parse(txtdriverpayment.Text.ToString()) + int.Parse(txtrent.Text.ToString()) + int.Parse(txtextra.Text.ToString());
           int amount = total - int.Parse(txtdiscount.Text);
           txttotal.Text = total.ToString();
           txtamount.Text = amount.ToString();
        
        }

        private void txtsilabordercharges_TextChanged(object sender, EventArgs e)
        {
            if (txtsilabordercharges.Text != "")
            {
                addaction();
            }
        }
        /// <summary>
        /// when submit button clicked
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
           if (validationcontrol())
                {
    
    
                    MessageBox.Show(cmbjobcode.SelectedValue.ToString());
                    OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                    oleDbConnection1.Open();
                    OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO quotationmastertable (quotationcode ,jobcode , jobpk , sillabordercharges , battabordercharges , driverpayment , rent , extra , total , discount , remark ,amount ) Values(@quotationcode ,@jobcode , @jobpk , @sillabordercharges , @battabordercharges , @driverpayment , @rent , @extra , @total , @discount , @remark ,@amount ) ", oleDbConnection1);
                    oleDbCommand1.Parameters.AddWithValue("@quotationcode", txtquotationno.Text);
                    oleDbCommand1.Parameters.AddWithValue("@jobcode", cmbjobcode.Text);
                    oleDbCommand1.Parameters.AddWithValue("@jobpk", cmbjobcode.SelectedValue);
                    oleDbCommand1.Parameters.AddWithValue("@sillabordercharges", int.Parse(txtsilabordercharges.Text));
                    oleDbCommand1.Parameters.AddWithValue("@battabordercharges", int.Parse(txtbattacharges.Text));
                    oleDbCommand1.Parameters.AddWithValue("@driverpayment", int.Parse(txtdriverpayment.Text));
                    oleDbCommand1.Parameters.AddWithValue("@rent", int.Parse(txtrent.Text));
                    oleDbCommand1.Parameters.AddWithValue("@extra", int.Parse(txtextra.Text));
                    oleDbCommand1.Parameters.AddWithValue("@total", int.Parse(txttotal.Text));
                    oleDbCommand1.Parameters.AddWithValue("@discount", int.Parse(txtdiscount.Text));
                    oleDbCommand1.Parameters.AddWithValue("@remark", txtremark.Text);
                    oleDbCommand1.Parameters.AddWithValue("@amount", int.Parse(txtamount.Text));
                    oleDbCommand1.CommandType = CommandType.Text;
                    oleDbCommand1.ExecuteNonQuery();
                    oleDbConnection1.Close();
                    MessageBox.Show(txtquotationno.Text);
                    this.Close();
    
                
            }
        }

        private void txtbattacharges_TextChanged(object sender, EventArgs e)
        {
            if (txtbattacharges.Text != "")
            {
                addaction();

            }
        }

        private void txtdriverpayment_TextChanged(object sender, EventArgs e)
        {
            if (txtdriverpayment.Text != "")
            {
                addaction();

            }

        }

        private void txtrent_TextChanged(object sender, EventArgs e)
        {
            if (txtrent.Text != "")
            {
                addaction();

            }
        }

        private void txtextra_TextChanged(object sender, EventArgs e)
        {
            if (txtextra.Text != "")
            {
                addaction();

            }
        }

        private void txtdiscount_TextChanged(object sender, EventArgs e)
        {
            if (txtdiscount.Text != "")
            {
                addaction();

            }

        }



    
    }
}
